<?php if ( ! defined('BASEPATH')) exit('No direct script access allowed'); 

class Caipu_mdl extends MY_Model {

	const DB_CAIPU = 'caipu';
	const DB_CAIPU_ATTACH = 'caipu_attach';

    public function __construct()
    {
		parent::__construct();
		$this->table = 'caipu';
    }

    //菜谱内容-相关菜谱***NEW
    public function get_caipu_related($keywords, $limit = NULL)
    {
    	$sql = 'SELECT t1.id,t1.title,t1.author,t1.authorid,t1.coverImage,t1.costtime FROM `'.$this->db->dbprefix.$this->table.'` AS t1 JOIN (SELECT ROUND(RAND() * ((SELECT MAX(id) FROM `'.$this->db->dbprefix.$this->table.'`)-(SELECT MIN(id) FROM `'.$this->db->dbprefix.$this->table.'`))+(SELECT MIN(id) FROM `'.$this->db->dbprefix.$this->table.'`)) AS id) AS t2 WHERE t1.id >= t2.id ORDER BY t1.id LIMIT '.intval($limit).'';
        return $this->db->query($sql);
    }
    /**
     * [菜谱信息]
     * @param  [type] $id [菜谱id]
     */
    public function get_caipu_by_id($id)
    {
    	$data = array();
		$this->db->select('*')->from(self::DB_CAIPU)->where('id', intval($id))->limit(1);
		$query = $this->db->get();
		if($query->num_rows() == 1)
		{
			$data = $query->row_array();
			$data['time'] = date("Y-m-d h:i", $data['time']);
			$data['coverImage'] = server_img($data['coverImage']);
		}
		$query->free_result();
		
		return $data;
    }
    /**
     * [菜谱附件 图片]
     * @param  [type] $id   [description]
     * @param  [type] $type [description]
     * @return [type]       [description]
     */
    public function get_caipu_attach_by_id($caipu_id, $type)
    {
		$this->db->select('location');
		$this->db->where('caipu_id', intval($caipu_id));
		$this->db->where('type', $this->db->escape_like_str($type));
		return $this->db->get(self::DB_CAIPU_ATTACH);
    }
    /**
     * [菜谱列表]
     * @param  [type]  $offset   [偏移量]
     * @param  [type]  $limit    [显示数量]
     * @param  [type]  $authorid [所属用户]
     * @param  integer $status   [状态 0隐藏，1显示]
     * @param  string  $orderby  [排序]
     */
    public function get_caipu_list($offset = NULL, $limit = NULL, $authorid = NULL, $status = NULL, $orderby = 'id DESC')
    {
		$this->db->select('*');
		
		if($status && is_numeric($status))
		{
			$this->db->where('status', intval($status));
		}
		else
		{
			//$this->db->where('status != 0');
		}

		if($authorid && is_numeric($authorid))
		{
			$this->db->where('authorid', intval($authorid));
		}

		if($limit && is_numeric($limit))
		{
			$this->db->limit(intval($limit));
		}
		
		if($offset && is_numeric($offset))
		{
			$this->db->offset(intval($offset));
		}
		$this->db->order_by($orderby); 
        return $this->db->get(self::DB_CAIPU);
    }
    //搜索
    public function get_caipu_search($keywords, $offset = NULL, $limit = NULL, $orderby = 'id DESC')
    {
    	$keywords = explode('+',$keywords);
    	$keywords_num = count($keywords);
		$this->db->select('*');

		for($i=0;$i<$keywords_num;$i++)
		{
			$key = urldecode($keywords[$i]);
			$this->db->like('title', $this->db->escape_like_str($key));
		}
		
		//$this->db->where('status', 1);

		if($limit && is_numeric($limit))
		{
			$this->db->limit(intval($limit));
		}
		
		if($offset && is_numeric($offset))
		{
			$this->db->offset(intval($offset));
		}

		$this->db->order_by($orderby); 

        return $this->db->get(self::DB_CAIPU);
    }
	//搜索微信
    public function get_caipu_weixin($keywords, $offset = NULL, $limit = NULL, $orderby = 'id DESC')
    {
    	$keywords = explode(' ',$keywords);
    	$keywords_num = count($keywords);
		$this->db->select('*');

		for($i=0;$i<$keywords_num;$i++)
		{
			$key = urldecode($keywords[$i]);
			$this->db->like('title', $this->db->escape_like_str($key));
		}
		
		//$this->db->where('status', 1);

		if($limit && is_numeric($limit))
		{
			$this->db->limit(intval($limit));
		}
		
		if($offset && is_numeric($offset))
		{
			$this->db->offset(intval($offset));
		}

		$this->db->order_by($orderby); 

        return $this->db->get(self::DB_CAIPU);
    }

    //食材内容-相关菜谱
    public function get_caipu_by_shicai($keywords, $offset = NULL, $limit = NULL, $orderby = 'id DESC')
    {
		$this->db->select('id,title,coverImage,infos,author,authorid,costtime');
		$this->db->like('materials', $this->db->escape_like_str($keywords));
		$this->db->where('status', 1);

		if($limit && is_numeric($limit))
		{
			$this->db->limit(intval($limit));
		}
		
		if($offset && is_numeric($offset))
		{
			$this->db->offset(intval($offset));
		}

		$this->db->order_by($orderby); 

        return $this->db->get(self::DB_CAIPU);
    }

    //分类标签-相关菜谱
    public function get_caipu_by_type($keywords = NULL, $offset = NULL, $limit = NULL, $orderby = 'id DESC')
    {
		$this->db->select('id,title,coverImage,infos,author,authorid');
		if($keywords && $keywords != 'all')
		{
			$this->db->like('class_name', $this->db->escape_like_str($keywords));
		}
		$this->db->where('status', 1);

		if($limit && is_numeric($limit))
		{
			$this->db->limit(intval($limit));
		}
		
		if($offset && is_numeric($offset))
		{
			$this->db->offset(intval($offset));
		}

		$this->db->order_by($orderby); 

        return $this->db->get(self::DB_CAIPU);
    }

    function update_views($id)
    {
		$this->db->set('views','views+1',FALSE);
		$this->db->where('id',$id);
    	return $this->db->update(self::DB_CAIPU);
    }
	//***********************后台专用*********************************************************************
	public function caipu_insert($arr)
	{
		$this->db->insert(self::DB_CAIPU, $arr);
		return $this->db->insert_id();
	}

	public function caipu_update($arr, $id)
	{
		return $this->db->update(self::DB_CAIPU, $arr, 'id = '.$id);
	}

	public function caipu_delete($id)
	{
		$this->db->select('*')->where('caipu_id', intval($id));
		$query = $this->db->get(self::DB_CAIPU_ATTACH)->result_array();
		if ($query)
		{
			$this->load->library('alioss');

			foreach($query as $file){
		        if (@file_exists('./upload/'.$file['location'])) @unlink('./upload/'.$file['location']);

		        $file['location'] = str_replace('/caipu', 'caipu', $file['location']);
		        $this->alioss->delete_object('ichufang',$file['location']);
		        $this->db->where('id',$file['id'])->delete(self::DB_CAIPU_ATTACH);
	    	}
		}
		return $this->db->where('id',$id)->delete(self::DB_CAIPU);
	}
}

/* End of file caipu_model.php */
/* Location: ./application/models/caipu_model.php */